#pragma once
#include "ZuiLib.h"
#include "LRefObject.h"
#include "ZMacro.h"

namespace ZuiLib {

class LRenderer :public LuaClass<ZRenderer>
{
public:
	void ctor(LuaFuncState& L)
	{
		L.error("cant create from lua");
	}

	int getAlpha(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetAlpha());
	}


	int setAlpha(LuaFuncState& L)
	{
		pThis->SetAlpha(L.arg(0).toUInt());
		return 0;
	}

	int multiplyAlpha(LuaFuncState& L)
	{
		pThis->MultiplyAlpha(L.arg(0).toUInt());
		return 0;
	}

	int getOutputSize(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L,pThis->GetOutputSize()));
	}

	int setLogicalSize(LuaFuncState& L)
	{
		pThis->SetLogicalSize(toSize(L.arg(0)));
		return 0;
	}

	int getLogicalSize(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetLogicalSize()));
	}

	int setViewport(LuaFuncState& L)
	{
		pThis->SetViewport(toRect(L.arg(0)));
		return 0;
	}

	int getViewport(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetViewport()));
	}


	int setClipRect(LuaFuncState& L)
	{
		pThis->SetClipRect(toRect(L.arg(0)));
		return 0;
	}

	int getClipRect(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetClipRect()));
	}

	int getScale(LuaFuncState& L)
	{
		float x, y;
		pThis->GetScale(&x,&y);
		return L.lreturn(x,y);
	}


	int setScale(LuaFuncState& L)
	{
		pThis->SetScale(L.arg(0).toNumber(), L.arg(1).toNumber());
		return 0;
	}

	int getDrawColor(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetDrawColor());
	}

	int setDrawColor(LuaFuncState& L)
	{
		pThis->SetDrawColor(toColor(L.arg(0)));
		return 0;
	}

	int drawLine(LuaFuncState& L)
	{
		pThis->DrawLine(L.arg(0).toInt(), L.arg(1).toInt(),
			L.arg(2).toInt(), L.arg(3).toInt());
		return 0;
	}

	int drawLines(LuaFuncState& L)
	{
		LuaTable lpoints = L.arg(0);
		if (!lpoints.isValid())
			return L.error("arg needs table");

		int count = lpoints.objLen();
		ZPoint * points = new ZPoint[count];

		for(int i=1;i<= count;++i)
		{
			LuaTable lpoint = lpoints.getTable(i);
			points[i - 1].x = lpoint.getTable("x").toInt();
			points[i - 1].y = lpoint.getTable("y").toInt();
		}
		pThis->DrawLines(points,count);
		delete[] points;
		return 0;
	}

	int drawRect(LuaFuncState& L)
	{
		ZRect rc;
		rc.x = L.arg(0).toInt();
		rc.y = L.arg(1).toInt();
		rc.w = L.arg(2).toInt();
		rc.h = L.arg(3).toInt();
		pThis->DrawRect(rc);
		return 0;
	}

	int drawRoundRect(LuaFuncState& L)
	{
		ZRect rc;
		rc.x = L.arg(0).toInt();
		rc.y = L.arg(1).toInt();
		rc.w = L.arg(2).toInt();
		rc.h = L.arg(3).toInt();
		pThis->DrawRoundRect(rc, L.arg(4).toInt());
		return 0;
	}

	int fillRect(LuaFuncState& L)
	{
		ZRect rc;
		rc.x = L.arg(0).toInt();
		rc.y = L.arg(1).toInt();
		rc.w = L.arg(2).toInt();
		rc.h = L.arg(3).toInt();
		pThis->FillRect(rc);
		return 0;
	}

	int fillRoundRect(LuaFuncState& L)
	{
		ZRect rc;
		rc.x = L.arg(0).toInt();
		rc.y = L.arg(1).toInt();
		rc.w = L.arg(2).toInt();
		rc.h = L.arg(3).toInt();
		pThis->FillRoundRect(rc, L.arg(4).toInt());
		return 0;
	}

	int drawImageString(LuaFuncState& L)
	{
		ZRect rc= toRect(L.arg(0));
		pThis->DrawImageString(rc,L.arg(1).toString());
		return 0;
	}

	//text,x,y,color,fontName,fontSize
	int drawText(LuaFuncState& L)
	{
        int fontSize = L.arg(5).toUInt();
        ZPoint pos(L.arg(1).toInt(), L.arg(2).toInt());

        if (fontSize <= 0)
            fontSize = 13;
		pThis->DrawText(L.arg(4).toString(), L.arg(0).toString() , fontSize, pos, L.arg(3).toUInt());
		return 0;
	}

	int drawBezier(LuaFuncState& L)
	{
		LuaTable point = L.arg(1);
		int n= point.objLen();
		Sint16 *mem = (Sint16*)SDL_malloc(2 * n * sizeof(Sint16));
		Sint16 * vx = mem;
		Sint16 * vy = mem + n;

		for (size_t i = 1; i <= n; i++)
		{
			LuaTable lpt = point.getTable(i);
			vx[i - 1] = lpt.getTable(1).toInt();
			vy[i - 1] = lpt.getTable(2).toInt();
		}

		pThis->DrawBezier(ConvertColor(L.arg(0).toUInt()),vx,vy,n, L.arg(2).toUInt());
		SDL_free(mem);
		return 0;
	}

    int drawEmoji(LuaFuncState& L)
    {
        ZRect rc = toRect(L.arg(1));
        pThis->DrawEmoji(L.arg(0).toUInt(),rc);
        return 0;
    }

	BEGIN_MAP_FUNC(LRenderer)
	DECLARE_FUNC(getAlpha),
	DECLARE_FUNC(setAlpha),
	DECLARE_FUNC(multiplyAlpha),
	DECLARE_FUNC(getOutputSize),
	DECLARE_FUNC(setLogicalSize),
	DECLARE_FUNC(getLogicalSize),
	DECLARE_FUNC(setViewport),
	DECLARE_FUNC(getViewport),
	DECLARE_FUNC(setClipRect),
	DECLARE_FUNC(getClipRect),
	DECLARE_FUNC(getScale),
	DECLARE_FUNC(setScale),
	DECLARE_FUNC(getDrawColor),
	DECLARE_FUNC(setDrawColor),
	DECLARE_FUNC(drawLine),
	DECLARE_FUNC(drawLines),
	DECLARE_FUNC(drawRect),
	DECLARE_FUNC(drawRoundRect),
	DECLARE_FUNC(fillRect),
	DECLARE_FUNC(fillRoundRect),
	DECLARE_FUNC(drawImageString),
	DECLARE_FUNC(drawText),
	DECLARE_FUNC(drawBezier),
    DECLARE_FUNC(drawEmoji),
	END_MAP_FUNC
};


}//namespace
